$(document).ready(function(){
    loadHotNews();//加载热点新闻
    loadTopNews();//加载最新新闻
    loadDigitalNewsList();//加载数码新闻
    loadDomesticNewsList();//加载国内新闻
    loadInternationalNewsList();//加载国际新闻
    loadSocialNewsList();//加载社会新闻
    loadArmNewsList();//加载军事新闻
    loadPublicWelfareNewsList();//加载公益新闻
    loadGovermentNewsList();//加载政务新闻
    $("#userLogin").click(function(){
    	checkUser();
    });
});

/******************************************start 加载热门新闻***********************************************************/
function loadHotNews(){
    $.ajax({
        type: "post",
        url: "front/news!hotNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**热点新闻**/
            $.each(data.hotNews, function(index, element){
                title = element.title.substr(0, 20) + "...";
                //&nbsp;&nbsp;&nbsp;&nbsp;[人气："+element.clickCount+"]
                $("#hotNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
                
            });
            
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
}

/******************************************start 加载最新新闻***********************************************************/
function loadTopNews(){
    $.ajax({
        type: "post",
        url: "front/news!topNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**最新新闻**/
            $.each(data.topNews, function(index, element){
                title = element.title.substr(0, 20) + "...";
                //	var publishTime=element.publishTime.substr(0,10);&nbsp;&nbsp;["+publishTime+"]
                $("#topNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
                
            });
            
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
}

/******************************************start 加载军事新闻***********************************************************/
function loadArmNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!armNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**军事新闻**/
            $.each(data.armNews, function(index, element){
                title = element.title.substr(0, 30) + "...";
                $("#armNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
                
            });
            
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
}

/******************************************start 加载国内新闻***********************************************************/
function loadDomesticNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!domesticNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**国内新闻**/
            $.each(data.domesticNews, function(index, element){
                title = element.title.substr(0, 12);
                $("#domesticNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
            });
            
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
}

/******************************************start 加载国际新闻***********************************************************/
function loadInternationalNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!internationalNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**国际新闻**/
            $.each(data.internationalNews, function(index, element){
                title = element.title.substr(0, 12);
                $("#internationalNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
            });
            
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
    
}

/******************************************start 加载社会新闻***********************************************************/
function loadSocialNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!socialNewsList",
        dataType: "json",
        async: true,
        success: function(data){
        
            /**社会新闻**/
            $.each(data.socialNews, function(index, element){
                title = element.title.substr(0, 12);
                $("#socialNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
            });
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
    
}

/******************************************start 加载政府新闻***********************************************************/
function loadGovermentNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!governmentNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**政务新闻**/
            $.each(data.governmentNews, function(index, element){
                title = element.title.substr(0, 12);
                $("#govermentNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
            });
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
    
}

/******************************************start 加载数码新闻***********************************************************/
function loadDigitalNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!digitalNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**数码新闻**/
            $.each(data.digitalNews, function(index, element){
                title = element.title.substr(0, 12);
                $("#digitalNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
            });
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
    
}

/******************************************start 加载公益新闻***********************************************************/
function loadPublicWelfareNewsList(){
    $.ajax({
        type: "post",
        url: "front/news!publicWelfareNewsList",
        dataType: "json",
        async: true,
        success: function(data){
            /**公益新闻**/
            $.each(data.publicWelfareNews, function(index, element){
                title = element.title.substr(0, 12);
                $("#publicWelfareNewsList").append("<li><a target='_blank' href='front/news!detail?id=" + element.newsId + "'>" + title + "</a></li>");
            });
        },
        error: function(data){
            alert("load error");
            return false;
        }
    });
    
}


/******************************************end 加载热门新闻***********************************************************/
function checkUser(){
    var username = $("#loginuserName").val();
    var password = $("#loginpassword").val();
    if (username == "") {
        alert("用户名不能为空！");
        $("#loginuserName").focus();
        return;
    }
    if (password == "") {
        alert("密码不能为空！");
        $("#loginpassword").focus();
        return;
    }
    doLogin();
}

/********************************登录****************************************/
function doLogin(){
    alert("login");
    var params = {
        "user.userName": $("#loginuserName").val(),
        "user.password": $("#loginpassword").val(),
        "user.isAdmin": 0
    };
    $.ajax({
        type: "post",
        url: "front/user!signIn",
        data: params,
        dataType: "json",
        success: function(data){
            if (data.result == 0) {
                $("#loginuserName").focus();
                alert("用户名或密码错误");
                
            }
            else {
                $("#loginuserName").val("");
                $("#loginpassword").val("");
                alert("登录成功！");
                $("#u_signIn").hide();
                $("#u_userInfor").html();
                $("#u_userInfor").append(data.user.userName);
                $("#u_signOut").show();
                
            }
            return true;
        },
        error: function(data){
			alert("login error!");
            return false;
        }
    });
    
}
